package com.ruoyi.parent.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.AliOssUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.parent.domain.ParentLoginVo;
import com.ruoyi.parent.domain.ParentVo;
import com.ruoyi.parent.service.IParentService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 家长管理Controller
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/parent/parent")
public class ParentController extends BaseController {
    @Autowired
    private IParentService parentService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询家长管理列表
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:list')")
    @GetMapping("/list")
    public TableDataInfo list(ParentVo parentVo) {
        startPage();
        List<ParentVo> list = parentService.selectParentList(parentVo);
        return getDataTable(list);
    }

    /**
     * 导出家长管理列表
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:export')")
    @Log(title = "家长管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParentVo parentVo) {
        List<ParentVo> list = parentService.selectParentList(parentVo);
        ExcelUtil<ParentVo> util = new ExcelUtil<ParentVo>(ParentVo.class);
        util.exportExcel(response, list, "家长管理数据");
    }

    /**
     * 获取家长管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(parentService.selectParentById(id));
    }

    /**
     * 新增家长管理
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:add')")
    @Log(title = "家长管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParentVo parentVo) {
        return toAjax(parentService.insertParent(parentVo));
    }

    /**
     * 修改家长管理
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:edit')")
    @Log(title = "家长管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParentVo parentVo) {
        return toAjax(parentService.updateParent(parentVo));
    }

    /**
     * 删除家长管理
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:remove')")
    @Log(title = "家长管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysUserService.deleteUserByIds(ids));
    }

    /**
     * 导入文件
     */
    @PreAuthorize("@ss.hasPermi('patriarch:patriarch:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ParentVo> util = new ExcelUtil<ParentVo>(ParentVo.class);
        List<ParentVo> parentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = parentService.importParent(parentList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ParentVo> util = new ExcelUtil<ParentVo>(ParentVo.class);
        util.importTemplateExcel(response, "家长数据");
    }

    /**
     * 获取验证码
     */
    @PostMapping("/sendCode")
    public AjaxResult sendPhoneCode(@RequestBody ParentLoginVo parentLoginVo) {
        return parentService.sendPhoneCode(parentLoginVo);
    }

    /**
     * 获取详细信息
     */
    @GetMapping("/info")
    public AjaxResult getUserInfo(@RequestParam String username) {
        System.out.println(parentService.getUserInfo(username));
        return AjaxResult.success(parentService.getUserInfo(username));
    }

    /**
     * 忘记密码
     */

    @PostMapping("/rePass")
    public AjaxResult rePassword(@RequestBody ParentLoginVo parentLoginVo) {
        return parentService.rePassword(parentLoginVo);
    }

    /**
     * 头像上传
     */

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) {
        System.out.println(file);
        String fileName = file.getOriginalFilename();
        //保证文件的名字唯一，防止覆盖，UUID+后缀名
        String fileName1 = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        String url = null;
        try {
            url = AliOssUtils.uploadFile(fileName1, file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success(url);
    }

    /**
     * 修改个人信息
     */

    @PostMapping("/changeInfo")
    public AjaxResult changeInfo(@RequestBody ParentLoginVo parentLoginVo) {
        return parentService.changeInfo(parentLoginVo);
    }

    /**
     * 修改密码
     */
    @PostMapping("/reNewPass")
    public AjaxResult changePass(@RequestBody ParentLoginVo parentLoginVo) {
        return parentService.changePass(parentLoginVo);
    }
}
